//
//  QSProblemInfoCell.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/26.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSProblemInfoCell.h"
static NSString *defaultCell = @"DefaultCell";

@implementation QSProblemInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configSubView];
    [self configCollectionView];
}

- (void)configSubView{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.layer.cornerRadius = 8;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(kRealValue(10));
        make.trailing.equalTo(self.mas_trailing).offset(kRealValue(-10));
        make.top.equalTo(self.mas_top).offset(kRealValue(10));
        make.bottom.equalTo(self.mas_bottom).offset(kRealValue(0));
    }];
    self.problemTypeLabel.backgroundColor = [UIColor colorWithHexString:@"#FFEDE9"];
    self.problemTypeLabel.layer.cornerRadius = 2;
    self.problemTypeLabel.layer.masksToBounds = YES;
}

- (void)configCollectionView{
    self.itemWH = kRealValue(kScreenWidth - 44 - 12) / 5;
    self.collectionViewH.constant = _itemWH;
   UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //上下间距
    flowLaout.minimumLineSpacing = 3;
    //左右间距
    flowLaout.minimumInteritemSpacing = 3;
    flowLaout.itemSize = CGSizeMake(_itemWH, _itemWH);
    flowLaout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.pagingEnabled = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    [self.collectionView reloadData];
    self.collectionView.collectionViewLayout = flowLaout;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:defaultCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:defaultCell forIndexPath:indexPath];
    for(UIView *subView in cell.contentView.subviews){
        if(subView){
            [subView removeFromSuperview];
        }
    }
    UIImageView *pic = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
//    pic.contentMode = UIViewContentModeScaleToFill;
    [pic sd_setImageWithURL:[NSURL URLWithString:_picArr[indexPath.item]]];
    [cell.contentView addSubview:pic];
    
    return cell;
}
- (void)setModel:(QSProblemDetailModel *)model{
    _model = model;
    self.originatorRightLabel.text =  [NSString stringWithFormat:@"%@-%@", model.createRole, model.createName];
    self.sendDateRightLabel.text = [NSDate dateWithTimestamp:model.createTime format:@"yyyy-MM-dd HH:mm"];
    self.problemTypeLabel.text = model.typeName;
    self.enterpriseRightLabel.text = model.companyName;
    self.contactRightLabel.text = model.contacts;
    self.phoneRightLabel.text = model.contactsPhone;
    self.streetRightLabel.text = model.townshipName;
    self.problemTitleLabel.text = model.title;
    self.ProblemDescriptionLabel.text = model.context;
//    [self.ProblemDescriptionLabel sizeToFit];
    NSString *str = _model.pic;
    if (str.length == 0) {
        self.collectionViewH.constant = _itemWH * 0;
        self.photoDescriptionLeftLabel.text = @"";
//        [self setupAutoHeightWithBottomView:self.ProblemDescriptionLabel bottomMargin:20];
    }else{
        self.photoDescriptionLeftLabel.text = @"图片描述：";
        _picArr = [str componentsSeparatedByString:@","]; //字符串按照【分隔成数组
        if (_picArr.count > 0 && _picArr.count <= 4) {
            self.collectionViewH.constant = _itemWH;
        }
        if (_picArr.count > 4 && _picArr.count <= 9) {
            self.collectionViewH.constant = _itemWH * 2 + 3;
        }
    }
    [self.collectionView reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentPage = indexPath.item;
    //展示
    [browser show];
}
#pragma mark - YBDataSource
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser{
    return _picArr.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index{
    YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:_picArr[index]];
    //    data.sourceObject = _pictureList[index].;
        return data;
}
@end
